package com.kaizeli.website.config;

import com.kaizeli.website.websocket.WarningWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置类
 * 用于配置WebSocket的端点和处理器
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private WarningWebSocketHandler warningWebSocketHandler;

    @Value("${websocket.server.port:8087}")
    private int websocketPort;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册WebSocket处理器，路径为 /ws/warning
        // 允许跨域访问（开发环境，生产环境需要指定具体域名）
        registry.addHandler(warningWebSocketHandler, "/ws/warning")
                .setAllowedOrigins("*");
        
        System.out.println("WebSocket配置完成 - 端口: " + websocketPort);
        System.out.println("WebSocket端点: ws://localhost:" + websocketPort + "/ws/warning");
    }
}

